package com.go.smsautosender;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.go.smsautosender.listview.FileListViewAdapter;
import com.go.smsautosender.listview.FileListViewData;


public class FileListViewActivity extends Activity implements OnClickListener {

	public static String ROOT_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();
	
	Bundle extra;
	Intent intent;
	
	private ListView ListView;
	private TextView tv_path;
	private Button btn_home, btn_back;
	
	FileListViewAdapter adapter;
	ArrayList<FileListViewData> arr_fileList;
	ArrayList<String> arr_fileInfo;
	File files;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.file_list);
		
		
		extra = new Bundle();
		intent = new Intent();
		
		extra.putInt("data", 1);
		intent.putExtras(extra);
		
		arr_fileList = new ArrayList<FileListViewData>();
		arr_fileInfo = new ArrayList<String>();
		initView();
		
		showFileList(ROOT_PATH);

		
		
	}
	
	private void initView() {

		ListView = (ListView) findViewById(R.id.Find_ListView);
		tv_path = (TextView) findViewById(R.id.path);

		btn_home = (Button) findViewById(R.id.btn_home);
		btn_back = (Button) findViewById(R.id.btn_back);

		btn_home.setOnClickListener(this);
		btn_back.setOnClickListener(this);

	}

	private void showFileList(String path) {
		arr_fileList.clear();
		tv_path.setText(path);
		files = new File(path);

		FilenameFilter dirFilter = new FilenameFilter() {
			public boolean accept(File dir, String name) {
				
				if (new File(dir, name).isDirectory()
						|| name.toLowerCase().endsWith(".txt")) {
					if (new File(dir, name).isHidden())
						return false;
					else
						return true;
				} else {
					return false;
				}
			}
		};

		try {
			if (files.listFiles().length > 0) {
				for (File file : files.listFiles(dirFilter)) {
					String imageType;

					if (file.isFile())
						imageType = "File";
					else
						imageType = "Directory";

					arr_fileList.add(new FileListViewData(imageType, file
							.getName(), file.getPath(), file.length()));
				}
			}
		} catch (NullPointerException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

		adapter = new FileListViewAdapter(FileListViewActivity.this,
				R.layout.list_item, arr_fileList);

		// files = null;
		// Collections.sort(arr_fileList, String.CASE_INSENSITIVE_ORDER);
		Collections.sort(arr_fileList, new ListSort(1));
		Collections.sort(arr_fileList, new ListSort(2));

		ListView.setAdapter(adapter);
		ListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		ListView.setOnItemClickListener(mItemClickListener);
		ListView.invalidate();
	}

	class ListSort implements Comparator<FileListViewData> {
		int type;

		ListSort(int _type) {
			type = _type;
		}

		public int compare(FileListViewData o1, FileListViewData o2) {
			int result = 0;
			
			if (type == 1)
				result = o1.FileName.compareToIgnoreCase(o2.FileName);
			else
				result = o1.ImageType.compareToIgnoreCase(o2.ImageType);

			return result;
		}
	}

	AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View view,
				final int position, long id) {
			final String type = adapter.getItemType(position);
			final String name = adapter.getItem(position);

			if (type.equals("File")) {
				 
				 intent.putExtra("path", files.getPath() + "/" + name);
				 intent.putExtra("filename", name);
				 
				 setResult(RESULT_OK, intent);
				 finish();
				 
			} else {
				showFileList(files.getPath() + "/" + name);
			}

		}
	};

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btn_back:
			if (files.getPath().equals("/mnt/sdcard"))
				break;

			if (files.getParent() != null)
				files = files.getParentFile();

			showFileList(files.getPath());
			break;
		case R.id.btn_home:
			showFileList(ROOT_PATH);
			break;
		
		}

	}
	
	public boolean onKeyDown( int KeyCode, KeyEvent event )
	{
	 
		if( KeyCode == KeyEvent.KEYCODE_BACK ){
			
			setResult(RESULT_CANCELED);
			finish();
			return true;
		}
		
		return super.onKeyDown( KeyCode, event );
	}

}
